package cn.gan.loveofsoul.controller;

import cn.gan.loveofsoul.commons.result.StatusCode;
import cn.gan.loveofsoul.service.DemoService;
import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 示例前端请求控制器
 */
@Slf4j
@RestController
public class DemoController {
    @Autowired
    private DemoService demoService;


    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    /** spring 提供的线程池 */
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;


    @GetMapping(path = {"/thread"})
    public void thread() {
        System.out.println("测试多线程执行：begin");
        try {
            if (threadPoolTaskExecutor.getThreadPoolExecutor().isShutdown()) {
                threadPoolTaskExecutor.initialize();
            }
            threadPoolTaskExecutor.execute(() -> {
                try {
                    System.out.println("当前线程名:"+Thread.currentThread().getName());
                    /*QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("id",1);
                    List<UserEntity> list = mqMapper.selectList(queryWrapper);
                    System.out.println("查询数据:"+ JSON.toJSONString(list));*/
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    //关闭线程池
                    threadPoolTaskExecutor.shutdown();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("测试多线程执行：end");
    }

    @GetMapping("/removeAllEmojis")
    public String removeAllEmojis(String name) {
        System.out.println(name);
        return EmojiParser.removeAllEmojis(name);
    }

    @GetMapping("/sendRedirect")
    public void sendRedirect(HttpServletResponse response) {
        try {
            response.setStatus(StatusCode.SEND_REDIRECT);
            response.sendRedirect("https://test.xsmh.chinavas.org.cn:9080/test");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
